package dao

import (
	"go_my_framework/app/core/db"
	"go_my_framework/app/model"
)

type ArticleCategoryDao interface {
	Create(categoryId int, articleId int) error
	GetList(articleId int) []model.ArticleCategory
	Del(id int) error
}

func NewArticleCategoryDaoImpl() ArticleCategoryDao {
	return new(ArticleCategoryDaoImpl)
}

type ArticleCategoryDaoImpl struct {
}

func (a *ArticleCategoryDaoImpl) Create(categoryId int, articleId int) error {
	data := model.ArticleCategory{
		CategoryId: categoryId,
		ArticleId:  articleId,
	}
	return db.Instance().Model(&model.ArticleCategory{}).Create(&data).Error
}

func (a *ArticleCategoryDaoImpl) GetList(articleId int) []model.ArticleCategory {
	var data []model.ArticleCategory
	db.Instance().Model(&model.ArticleCategory{}).Where("article_id", articleId).Find(&data)
	return data
}

func (a *ArticleCategoryDaoImpl) Del(id int) error {
	return db.Instance().
		Where("article_id = ?", id).
		Delete(model.ArticleCategory{}).
		Error
}
